package com.snacks;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.snacks.data.DataFromHttpSever;
import com.snacks.data.URLconstan;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemDetail extends Activity implements OnClickListener{
	private TextView tvName, tvAddress, tvCreateUser, tvCreateDate, tvPhone, tvDescription;
	
	private Button btCall, btLocate, btBack;
	private ImageView imgView;
	public String Mobile, ToaDo, id;
	public String strName, strAddress, strThumbnail, strCreateUser, strCreateDate, strPhone, strDescription, strLatitude, strLongitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.itemdetail);

		tvName = (TextView) findViewById(R.id.tvName);
		tvAddress = (TextView) findViewById(R.id.tvAddress);
		
		tvCreateUser = (TextView) findViewById(R.id.tvCreateUser);
		tvCreateDate = (TextView) findViewById(R.id.tvCreateDate);
		tvPhone = (TextView) findViewById(R.id.tvPhone);
		tvDescription = (TextView) findViewById(R.id.tvDescription);
		

		imgView = (ImageView) findViewById(R.id.imgView);

		btCall = (Button) findViewById(R.id.btCall_Detail);
		btLocate = (Button) findViewById(R.id.btLocate_Detail);
		btBack = (Button) findViewById(R.id.btBack);
		
		Intent intent2 = getIntent();
		HashMap<String, String> hashmap = (HashMap<String, String>) intent2.getSerializableExtra("restaurant");
		id = hashmap.get("id");
		
		
		// parse json data
		ArrayList<NameValuePair> rq;
		rq = new ArrayList<NameValuePair>();
		rq.add(new BasicNameValuePair("id", id.trim()));
		DataFromHttpSever dt = new DataFromHttpSever();
		String data = dt.getDataFromHttpSever(rq, URLconstan.getItemService());
		
		Log.i("log_tag", "Parse JSON Data");	    	
    	try{
    		JSONArray jArray = new JSONArray(data);	
    		
    		for(int i=0;i<jArray.length();i++)
    		{
    			JSONObject e = jArray.getJSONObject(i);
    			Log.i("log_tag","ID: "+e.getString("id")+", Ten The Loai: "+ e.getString("name"));
    			strName = e.getString("name");
    			strAddress = e.getString("address") + ", " + e.getString("area");
    			strDescription = e.getString("description");
    			strPhone = e.getString("phonenumber");
    			strThumbnail = e.getString("thumbnail");
    			strCreateUser = e.getString("createuser");
    			strCreateDate = e.getString("createdate");
    			strLatitude = e.getString("latitude");
    			strLongitude = e.getString("longitude");
    				      
    		}
    	}
    	catch(JSONException e){
  	      	Log.e("log_tag", "Error parsing data "+e.toString());	    			     
  	    } 	
		
    	tvName.setText(strName);
    	tvAddress.setText(strAddress);
    	tvPhone.setText(strPhone);
    	tvDescription.setText(Html.fromHtml(strDescription));
    	tvCreateUser.setText(strCreateUser);
    	tvCreateDate.setText(strCreateDate);		

		String LinkImage = URLconstan.getServer() +  "/uploads/" + strThumbnail;
		try {
			URL url = new URL(LinkImage);
			Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
			imgView.setImageBitmap(bmp);
		} catch (Exception e) {
			// TODO: handle exception
		}
		btCall.setOnClickListener(this);
		btLocate.setOnClickListener(this);
		btBack.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btCall_Detail:
			String MobileNumber = "tel:" + Mobile.substring(6).trim();
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri
					.parse(MobileNumber));
			startActivity(callIntent);
			break;
		case R.id.btLocate_Detail:
			Intent intentmap = new Intent(getBaseContext(), Map.class);
			Bundle mBundle = new Bundle();
			mBundle.putString("id", id);
			mBundle.putString("latitude", strLatitude);
			mBundle.putString("longitude", strLongitude);
			intentmap.putExtras(mBundle);
			startActivity(intentmap);
			break;
		case R.id.btBack:
			finish();
			break;
		default:
			break;
		}
		
	}
}
